Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
The tslint npm package is a static analysis tool that checks TypeScript code for readability, maintainability, and functionality errors. It is widely used to enforce a consistent code style by checking the code against a set of linting rules.
Linting TypeScript Files
This feature allows you to lint TypeScript files by specifying a configuration file and a pattern to match files. The command will process all TypeScript files in the 'src' directory and its subdirectories.
tslint -c tslint.json 'src/**/*.ts'
Fixing Linting Errors Automatically
This feature automatically fixes linting errors that can be corrected without human intervention. It is useful for fixing simple issues like whitespace or semicolon usage.
tslint --fix -c tslint.json 'src/**/*.ts'
Custom Rules
This feature allows you to use custom linting rules in addition to the predefined rules. You can specify a directory containing custom rule definitions to be applied to your code.
tslint -c tslint.json 'src/**/*.ts' --rules-dir custom_rules
ESLint is a popular linting tool for JavaScript and TypeScript. It is highly configurable and extendable, with a large ecosystem of plugins. ESLint has effectively replaced TSLint as the preferred linter for TypeScript after TSLint's deprecation.
Prettier is an opinionated code formatter that supports many languages, including TypeScript. While it does not perform static code analysis, it formats code to a consistent style. Prettier can be used alongside linters like ESLint.
Stylelint is a modern linter that helps you avoid errors and enforce conventions in your stylesheets. Although it is primarily used for CSS, it can be used in conjunction with PostCSS to lint SCSS, Sass, Less, and other CSS-like languages.
A linter for the TypeScript language.
npm install tslint -g
npm install tslint
next
distributionThe next
branch of the TSLint repo tracks the latest TypeScript
compiler and allows you to lint TS code that uses the latest features of the language. Releases from this branch
are published to npm with the next
dist-tag, so you can get the latest dev version of TSLint via
npm install tslint@next
.
Please first ensure that the TypeScript source files compile correctly.
usage: tslint [options] [file ...]
Options:
-c, --config configuration file
-o, --out output file
-r, --rules-dir rules directory
-s, --formatters-dir formatters directory
-t, --format output format (prose, json) [default: "prose"]
By default, configuration is loaded from tslint.json
, if it exists in the current path, or the user's home directory, in that order.
tslint accepts the following command-line options:
-c, --config:
The location of the configuration file that tslint will use to
determine which rules are activated and what options to provide
to the rules. If no option is specified, the config file named
tslint.json is used, so long as it exists in the path.
The format of the file is { rules: { /* rules list */ } },
where /* rules list */ is a key: value comma-seperated list of
rulename: rule-options pairs. Rule-options can be either a
boolean true/false value denoting whether the rule is used or not,
or a list [boolean, ...] where the boolean provides the same role
as in the non-list case, and the rest of the list are options passed
to the rule that will determine what it checks for (such as number
of characters for the max-line-length rule, or what functions to ban
for the ban rule).
-o, --out:
A filename to output the results to. By default, tslint outputs to
stdout, which is usually the console where you're running it from.
-r, --rules-dir:
An additional rules directory, for user-created rules.
tslint will always check its default rules directory, in
node_modules/tslint/build/rules, before checking the user-provided
rules directory, so rules in the user-provided rules directory
with the same name as the base rules will not be loaded.
-s, --formatters-dir:
An additional formatters directory, for user-created formatters.
Formatters are files that will format the tslint output, before
writing it to stdout or the file passed in --out. The default
directory, node_modules/tslint/build/formatters, will always be
checked first, so user-created formatters with the same names
as the base formatters will not be loaded.
-t, --format:
The formatter to use to format the results of the linter before
outputting it to stdout or the file passed in --out. The core
formatters are prose (human readable) and json (machine readable),
and prose is the default if this option is not used. Additional
formatters can be added and used if the --formatters-dir option
is set.
--help:
Prints this help message.
var fileName = "Specify file name";
var configuration = {
rules: {
"variable-name": true,
"quotemark": [true, "double"]
}
};
var options = {
formatter: "json",
configuration: configuration,
rulesDirectory: "customRules/",
formattersDirectory: "customFormatters/"
};
var Linter = require("tslint");
var fs = require("fs");
var contents = fs.readFileSync(fileName, "utf8");
var ll = new Linter(fileName, contents, options);
var result = ll.lint();
A sample configuration file with all options is available here.
align
enforces vertical alignment. Rule options:
"parameters"
checks alignment of function parameters."arguments"
checks alignment of function call arguments."statements"
checks alignment of statements.ban
bans the use of specific functions. Options are ["object", "function"] pairs that ban the use of object.function()class-name
enforces PascalCased class and interface names.comment-format
enforces rules for single-line comments. Rule options:
"check-space"
enforces the rule that all single-line comments must begin with a space, as in // comment
///
are also allowed, for things such as ///<reference>
"check-lowercase"
enforces the rule that the first non-whitespace character of a comment must be lowercase, if applicable"check-uppercase"
enforces the rule that the first non-whitespace character of a comment must be uppercase, if applicablecurly
enforces braces for if
/for
/do
/while
statements.eofline
enforces the file to end with a newline.forin
enforces a for ... in
statement to be filtered with an if
statement.*indent
enforces indentation with tabs or spaces. Rule options (one is required):
"tabs"
enforces consistent tabs"spaces"
enforces consistent spacesinterface-name
enforces the rule that interface names must begin with a capital 'I'jsdoc-format
enforces basic format rules for jsdoc comments -- comments starting with /**
/**
and end with */
label-position
enforces labels only on sensible statements.label-undefined
checks that labels are defined before usage.max-line-length
sets the maximum length of a line.member-access
enforces using explicit visibility on class membersmember-ordering
enforces member ordering. Rule options:
public-before-private
All public members must be declared before private membersstatic-before-instance
All static members must be declared before instance membersvariables-before-functions
All variables needs to be declared before functionsno-any
diallows usages of any
as a type decoration.no-arg
disallows access to arguments.callee
.no-bitwise
disallows bitwise operators.no-conditional-assignment
disallows any type of assignment in any conditionals. This applies to do-while
, for
, if
, and while
statements.no-console
disallows access to the specified functions on console
. Rule options are functions to ban on the console variable.no-consecutive-blank-lines
disallows having more than one blank line in a row in a fileno-construct
disallows access to the constructors of String
, Number
, and Boolean
.no-constructor-vars
disallows the public
and private
modifiers for constructor parameters.no-debugger
disallows debugger
statements.no-duplicate-key
disallows duplicate keys in object literals.no-duplicate-variable
disallows duplicate variable declarations in the same block scope.no-shadowed-variable
disallows shadowed variable declarations.no-empty
disallows empty blocks.no-eval
disallows eval
function invocations.no-inferrable-types
disallows explicit type declarations for variables or parameters initialized to a number, string, or booleanno-internal-module
disallows internal module
, use namespace
instead.no-require-imports
disallows require() style importsno-string-literal
disallows object access via string literals.no-switch-case-fall-through
disallows falling through case statements.no-trailing-comma
disallows trailing comma within object literals.no-trailing-whitespace
disallows trailing whitespace at the end of a line.no-unreachable
disallows unreachable code after break
, catch
, throw
, and return
statements.no-unused-expression
disallows unused expression statements, that is, expression statements that are not assignments or function invocations (and thus no-ops).no-unused-variable
disallows unused imports, variables, functions and private class members.
"check-parameters"
disallows unused function and constructor parameters.
no-use-before-declare
disallows usage of variables before their declaration.no-var-keyword
disallows usage of the var
keyword, use let
or const
instead.no-var-requires
disallows the use of require statements except in import statements, banning the use of forms such as var module = require("module")
one-line
enforces the specified tokens to be on the same line as the expression preceding it. Rule options:
"check-catch"
checks that catch
is on the same line as the closing brace for try
"check-else"
checks that else
is on the same line as the closing brace for if
"check-open-brace"
checks that an open brace falls on the same line as its preceding expression."check-whitespace"
checks preceding whitespace for the specified tokens.quotemark
enforces consistent single or double quoted string literals. Rule options (at least one of "double"
or "single"
is required):
"single"
enforces single quotes"double"
enforces double quotes"avoid-escape"
allows you to use the "other" quotemark in cases where escaping would normally be required. For example, [true, "double", "avoid-escape"]
would not report a failure on the string literal 'Hello "World"'
.radix
enforces the radix parameter of parseInt
semicolon
enforces semicolons at the end of every statement.sort-object-literal-keys
checks that keys in object literals are declared in alphabetical orderswitch-default
enforces a default
case in switch
statements.triple-equals
enforces === and !== in favor of == and !=.typedef
enforces type definitions to exist. Rule options:
"call-signature"
checks return type of functions"parameter"
checks type specifier of function parameters"property-declaration"
checks return types of interface properties"variable-declaration"
checks variable declarations"member-variable-declaration"
checks member variable declarationstypedef-whitespace
enforces spacing whitespace for type definitions. Each rule option requires a value of "space"
or "nospace"
to require a space or no space before the type specifier's colon. Rule options:
"call-signature"
checks return type of functions"index-signature"
checks index type specifier of indexers"parameter"
checks function parameters"property-declaration"
checks object property declarations"variable-declaration"
checks variable declarationuse-strict
enforces ECMAScript 5's strict mode
check-module
checks that all top-level modules are using strict modecheck-function
checks that all top-level functions are using strict modevariable-name
allows only camelCased or UPPER_CASED variable names. Rule options:
"allow-leading-underscore"
allows underscores at the beginnning."allow-trailing-underscore"
allows underscores at the end.whitespace
enforces spacing whitespace. Rule options:
"check-branch"
checks branching statements (if
/else
/for
/while
) are followed by whitespace"check-decl"
checks that variable declarations have whitespace around the equals token"check-operator"
checks for whitespace around operator tokens"check-module"
checks for whitespace in import & export statements"check-separator"
checks for whitespace after separator tokens (,
/;
)"check-type"
checks for whitespace before a variable type specification"check-typecast"
checks for whitespace between a typecast and its targetYou can enable/disable TSLint or a subset of rules within a file with the following comment rule flags:
/* tslint:disable */
- Disable all rules for the rest of the file/* tslint:enable */
- Enable all rules for the rest of the file/* tslint:disable:rule1 rule2 rule3... */
- Disable the listed rules for the rest of the file/* tslint:enable:rule1 rule2 rule3... */
- Enable the listed rules for the rest of the fileRules flags enable or disable rules as they are parsed. A rule is enabled or disabled until a later directive commands otherwise. Disabling an already disabled rule or enabling an already enabled rule has no effect.
For example, imagine the directive /* tslint:disable */
on the first line of a file, /* tslint:enable:ban class-name */
on the 10th line and /* tslint:enable */
on the 20th. No rules will be checked between the 1st and 10th lines, only the ban
and class-name
rules will be checked between the 10th and 20th, and all rules will be checked for the remainder of the file.
TSLint ships with a set of core rules that can be configured. However, users are also allowed to write their own rules, which allows them to enforce specific behavior not covered by the core of TSLint. TSLint's internal rules are itself written to be pluggable, so adding a new rule is as simple as creating a new rule file named by convention. New rules can be written in either TypeScript or Javascript; if written in TypeScript, the code must be compiled to Javascript before invoking TSLint.
Rule names are always camel-cased and must contain the suffix Rule
. Let us take the example of how to write a new rule to forbid all import statements (you know, for science). Let us name the rule file noImportsRule.ts
. Rules can be referenced in tslint.json
in their dasherized forms, so "no-imports": true
would turn on the rule.
Now, let us first write the rule in TypeScript. At the top, we reference TSLint's definition file and the definition file for TypeScript's language services. The exported class must always be named Rule
and extend from Lint.Rules.AbstractRule
.
/// <reference path='../node_modules/tslint/typings/typescriptServices.d.ts' />
/// <reference path='../node_modules/tslint/lib/tslint.d.ts' />
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "import statement forbidden";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoImportsWalker(sourceFile, this.getOptions()));
}
}
The walker takes care of all the work.
class NoImportsWalker extends Lint.RuleWalker {
public visitImportDeclaration(node: ts.ImportDeclaration) {
// create a failure at the current position
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
// call the base version of this visitor to actually parse this node
super.visitImportDeclaration(node);
}
}
Given a walker, TypeScript's parser visits the AST using the visitor pattern. So the rule walkers only need to override the appropriate visitor methods to enforce its checks. For reference, the base walker can be found in syntaxWalker.ts.
We still need to hook up this new rule to TSLint. First make sure to compile noImportsRule.ts
: tsc -m commonjs --noImplicitAny noImportsRule.ts tslint.d.ts
. Then, if using the CLI, provide the directory that contains this rule as an option to --rules-dir
. If using TSLint as a library or via grunt-tslint
, the options
hash must contain "rulesDirectory": "..."
. If you run the linter, you'll see that we have now successfully banned all import statements via TSLint!
Now, let us rewrite the same rule in Javascript.
function Rule() {
Lint.Rules.AbstractRule.apply(this, arguments);
}
Rule.prototype = Object.create(Lint.Rules.AbstractRule.prototype);
Rule.prototype.apply = function(sourceFile) {
return this.applyWithWalker(new NoImportsWalker(sourceFile, this.getOptions()));
};
function NoImportsWalker() {
Lint.RuleWalker.apply(this, arguments);
}
NoImportsWalker.prototype = Object.create(Lint.RuleWalker.prototype);
NoImportsWalker.prototype.visitImportDeclaration = function (node) {
// create a failure at the current position
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), "import statement forbidden"));
// call the base version of this visitor to actually parse this node
Lint.RuleWalker.prototype.visitImportDeclaration.call(this, node);
};
exports.Rule = Rule;
As you can see, it's a pretty straightforward translation from the equivalent TypeScript code.
Finally, core rules cannot be overwritten with a custom implementation, and rules can also take in options (retrieved via this.getOptions()
).
Just like rules, additional formatters can also be supplied to TSLint via --formatters-dir
on the CLI or formattersDirectory
option on the library or grunt-tslint
. Writing a new formatter is simpler than writing a new rule, as shown in the JSON formatter's code.
/// <reference path='../node_modules/tslint/typings/typescriptServices.d.ts' />
/// <reference path='../node_modules/tslint/lib/tslint.d.ts' />
export class Formatter extends Lint.Formatters.AbstractFormatter {
public format(failures: Lint.RuleFailure[]): string {
var failuresJSON = failures.map((failure: Lint.RuleFailure) => failure.toJson());
return JSON.stringify(failuresJSON);
}
}
Such custom formatters can also be written in Javascript. Additionally, formatter files are always named with the suffix Formatter
, and referenced from TSLint without its suffix.
To develop TSLint simply clone the repository, install dependencies and run grunt:
git clone git@github.com:palantir/tslint.git
npm install
grunt
grunt
to build the latest sourcesnpm publish
v2.5.1
AbstractRule
and AbstractFormatter
are now abstract classes (#631)
Lint.abstract()
is now deprecatedFAQs
An extensible static analysis linter for the TypeScript language
The npm package tslint receives a total of 1,329,058 weekly downloads. As such, tslint popularity was classified as popular.
We found that tslint demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.